package com.hardware.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hardware.common.BaseContext;
import com.hardware.common.R;
import com.hardware.entity.ShoppingCart;
import com.hardware.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.hardware.utils.RedisConstants.CACHE_SHOPPINGCART_KEY;

/**
 * 前台：用户购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     *通过当前用户Id,查询购物车内数据
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = CACHE_SHOPPINGCART_KEY)
    public R<List<ShoppingCart>> list(){
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getUserId() != null,ShoppingCart::getUserId,shoppingCart.getUserId());
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }

    /**
     * 添加商品到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    @CacheEvict(value = CACHE_SHOPPINGCART_KEY,allEntries = true)
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断当前商品,是否存在购物车,是:数量加1,否:添加购物车
        LambdaQueryWrapper<ShoppingCart> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        lqw1.eq(ShoppingCart::getShopId,shoppingCart.getShopId());

        ShoppingCart one = shoppingCartService.getOne(lqw1);
        if(one != null){
            //存在购物车,商品数量加1
            one.setNumber(one.getNumber() + 1);

            shoppingCartService.updateById(one);
            return R.success(one);
        }
        //不存在,添加进购物车
        shoppingCart.setNumber(1);
        shoppingCartService.save(shoppingCart);

        return R.success(shoppingCart);

    }

    /**
     *减少购物车商品数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    @CacheEvict(value = CACHE_SHOPPINGCART_KEY,allEntries = true)
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断当前商品,是否存在购物车,是:数量减1,否:返回错误信息
        LambdaQueryWrapper<ShoppingCart> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        lqw1.eq(ShoppingCart::getShopId,shoppingCart.getShopId());

        ShoppingCart one = shoppingCartService.getOne(lqw1);
        if(one != null){
            //存在购物车,判断数量
            //if only one delete goods
            if(one.getNumber() == 1){
                shoppingCartService.remove(lqw1);
                one.setNumber(0);
            }else{
                // 商品数量减1
                one.setNumber(one.getNumber() - 1);
                shoppingCartService.updateById(one);
            }
            return R.success(one);
        }
        return R.error("购物车中没有该商品");
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    @CacheEvict(value = CACHE_SHOPPINGCART_KEY,allEntries = true)
    public R<String> clean(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(lqw);
        return R.success("删除成功");
    }
}
